package com.thought.wxsession.chain;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.thought.wxsession.cons.MsgStatus;
import com.thought.wxsession.cons.MsgType;
import com.thought.wxsession.data.msg.Emotion;
import com.thought.wxsession.data.msg.Image;
import com.thought.wxsession.entity.WxMsg;
import com.thought.wxsession.service.IWxMsgService;
import com.thought.wxsession.utils.SessionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

import static com.thought.wxsession.cons.ChainCons.MSG_ID;
import static com.thought.wxsession.cons.ChainCons.MSG_TYPE;
import static com.thought.wxsession.cons.EmoType.STATIC;
import static com.thought.wxsession.cons.MsgType.emotion;
import static com.thought.wxsession.cons.ParsingStatus.PARSING_FAILED;
import static com.thought.wxsession.cons.ParsingStatus.PARSING_SUCCESSFULLY;

/**
 * @Author：zhuqinyue
 * @Package：com.thought.wxsession.chain
 * @Project：session-save
 * @name：TextChain
 * @Date：2022/12/26 10:27
 * @Filename：TextChain
 */
@Component
@Slf4j
public class EmotionChain implements Command {


    @Autowired
    private IWxMsgService wxMsgService;

    @Override
    public boolean execute(Context context){

        String msgType = String.valueOf(context.get(MSG_TYPE));
        Long id = (Long) context.get(MSG_ID);
        if (emotion.getMsgType().equals(msgType)) {
            log.info("进入emotion消息责任链");
            WxMsg wxMsgRes = wxMsgService.getById(id);
            Optional.ofNullable(wxMsgRes).ifPresent(wxMsg->{
                //如果成功拉取了消息
                try {
                    if (MsgStatus.SUCCESS.getCode().equals(wxMsg.getErrorCode())) {
                        Emotion emo = JSONObject.parseObject(wxMsg.getContent(), Emotion.class);
                        String sdkfileid = emo.getSdkfileid();
                        SessionUtils sessionUtils = new SessionUtils();
                        String suffix =  STATIC.getCode() == emo.getType() ? ".png" : ".gif";
                        String fileName = wxMsg.getMsgid() + suffix;
                        String filePath = sessionUtils.downMedia(sdkfileid, "file/" + fileName,wxMsg.getSdk());
                        wxMsg.setFilePath(filePath);
                        wxMsg.setParsingStatus(PARSING_SUCCESSFULLY.getCode());
                        wxMsg.setParsingDesc(PARSING_SUCCESSFULLY.getDesc());
                        wxMsg.setFileName(fileName);
                    }
                } catch (Exception e) {
                    wxMsg.setParsingStatus(PARSING_FAILED.getCode());
                    wxMsg.setParsingDesc(ExceptionUtil.stacktraceToString(e));
                }finally {
                    if(wxMsg!=null)
                        wxMsgService.updateById(wxMsg);
                }
            });
            return true;
        }
        //返回false表示不执行
        return false;
    }
}
